<template>
	<view class="content b-t">
		<uni-search-bar @confirm="search" style="z-index: 11111;" @input="input" v-if="type==1"></uni-search-bar>
		<view class="list b-b" v-for="(item, index) in addressList" :key="index" @click="checkAddress(item)">
			<view class="wrapper">
				<view class="address-box">
					<text v-if="item.status==1&&type!=1" class="tag">默认</text>
					<text class="address">{{item.address}} {{item.area}}</text>
				</view>
				<view class="u-box">
					<text class="name">{{item.name}}</text>
					<text class="mobile">{{item.phone}}</text>
				</view>
			</view>
			<text class="yticon icon-bianji" v-if="type!=1" @click.stop="addAddress('edit', item.id)"></text>
			<text style="width: 30rpx;" v-if="type!=1"></text>
			<text class="yticon icon-fork" v-if="type!=1" @click.stop="addAddress('del', item.id)"></text>
		</view>
		<button class="add-btn" v-if="type!=1" @click="addAddress('add')">新增地址</button>
		<noneDate v-if="addressList.length==0">
		</noneDate>
	</view>
</template>

<script>
	import noneDate from '@/components/noneDate/noneDate.vue'
	export default {
		components: {
			noneDate
		},
		data() {
			return {
				source: 0,
				addressList: [],
				page: 1,
				limit: 10,
				name: '',
				type: 2
			}
		},
		onLoad(option) {
			this.source = option.source;
			if (option.type) {
				this.type = option.type;
			}
		},
		onShow() {
			this.addressList = []
			this.page = 1
			this.init()
		},
		onReachBottom() {
			if (this.type == 1) {
				this.getUser_address1(this.page, this.limit)
			} else {
				this.getUser_address(this.page, this.limit)
			}
		},
		methods: {
			init() {
				if (this.type == 1) {
					this.getUser_address1(this.page, this.limit)
				} else {
					this.getUser_address(this.page, this.limit)
				}
			},
			input(val) {
				this.name = val
				this.addressList = []
				this.page = 1
				this.getUser_address1(this.page, this.limit)
			},
			getUser_address(page, limit) {
				this.$fun.ajax.post('/user/Address', {
					page,
					limit
				}).then(res => {
					if (res.data.length > 0) {
						this.addressList.push(...res.data);
						// this.addressList = res.data
						this.page++
					} else {
						if (this.page != 1) {
							this.$fun.msg('没有更多数据了...')
						}
					}
				})
			},
			getUser_address1(page, limit) {
				this.$fun.ajax.post('user/fuwu', {
					page,
					limit,
					name: this.name
				}).then(res => {
					if (res.data.length > 0) {
						this.addressList.push(...res.data);
						// this.addressList = res.data
						this.page++
					} else {
						if (this.page != 1) {
							this.$fun.msg('没有更多数据了...')
						}
					}
				})
			},
			//选择地址
			checkAddress(item) {
				//console.log(111)
				if (this.source == 1) {

					//this.$api.prePage()获取上一页实例，在App.vue定义
					// this.$prePage().addressData ;
					let pages = getCurrentPages();
					console.log(getCurrentPages())
					let prePage = pages[pages.length - 2];
					if (this.type == 1) {
						prePage.addressData1 = item
						uni.setStorageSync('addressData1', JSON.stringify(item))
					} else {
						prePage.addressData = item
						uni.setStorageSync('addressData', JSON.stringify(item))
					}
					uni.navigateBack()
				}
			},
			addAddress(type, item) {
				//console.log(type)
				//console.log(type == 'del')
				if (type == 'del') {
					uni.showModal({
						content: '确定要删除该地址吗',
						success: (e) => {
							if (e.confirm) {
								this.$fun.ajax.post('user/deladdr', {
									id: item
								}).then(res => {
									uni.showToast({
										title: res.msg,
										icon: "none"
									})
									if (res.code == 1) {
										this.addressList = []
										this.page = 1
										this.init()
									}
								})
							}
						}
					});
				} else
					uni.navigateTo({
						url: `/pageA/address/editAddress?type=${type}&data=${JSON.stringify(item)}`
					})
			},
			//添加或修改成功之后回调
			refreshList(data, type) {
				//添加或修改后事件，这里直接在最前面添加了一条数据，实际应用中直接刷新地址列表即可
				this.addressList.unshift(data);

				//console.log(data, type);
			}
		}
	}
</script>

<style lang='scss'>
	page {
		padding-bottom: 120upx;
	}

	.u-navbar__content__right {
		/* #ifdef MP-WEIXIN */
		margin-right: 180rpx;
		font-size: 24rpx;
		/* #endif */
	}

	.content {
		position: relative;
		padding-top: 10rpx;
	}

	.list {
		display: flex;
		align-items: center;
		padding: 20upx 30upx;
		background: #fff;
		position: relative;
	}

	.wrapper {
		display: flex;
		flex-direction: column;
		flex: 1;
	}

	.address-box {
		display: flex;
		align-items: center;

		.tag {
			font-size: 24upx;
			color: $base-color;
			margin-right: 10upx;
			background: #fffafb;
			border: 1px solid #ffb4c7;
			border-radius: 4upx;
			padding: 4upx 10upx;
			line-height: 1;
		}

		.address {
			font-size: 30upx;
			color: $font-color-dark;
		}
	}

	.u-box {
		font-size: 28upx;
		/* color: $font-color-light; */
		margin-top: 16upx;

		.name {
			margin-right: 30upx;
		}
	}

	.icon-bianji {
		display: flex;
		align-items: center;
		height: 80upx;
		font-size: 40upx;
		/* color: $font-color-light; */
		padding-left: 30upx;
	}

	.add-btn {
		position: fixed;
		left: 30upx;
		right: 30upx;
		bottom: 16upx;
		z-index: 95;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690rpx;
		height: 88rpx;
		background: $base-color;
		border-radius: 44rpx;
		font-size: 32upx;
		color: #fff;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}

	.isaddress {
		width: 690rpx;
		height: 168rpx;
		background: #F6F6F6;
		border-radius: 24rpx;
		padding: 30rpx 24rpx;
		font-weight: 400;
		font-size: 28rpx;
		color: #333333;
		margin: 30rpx auto 0;
	}
</style>